// index.js
const regeneratorRuntime = require('../../lib/runtime');
const wx = require('../../lib/wx');
const Api = require('../../utils/api.js');
const wxapp = require('../../lib/wxapp');
const Util = require('../../utils/util.js');

// 获取应用实例
const app = getApp();
Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    circular: true,
    carNum:0,
    special:[],
    bannersList: [
      {
        url: '../discountDetail/discountDetail',
        img: '../../images/index_banner1.png',
      },
    ],
    productList: [
      {
        imgUrl: '../../images/no_image.png',
        url: `../productDetail/productDetail?isGroup=true`,
        people: 2,
        title: '品牌',
        subTitle: '标题',
        current_price: '00.00',
        orign_price: '00.00',
        joined: 0,
      },
    ],
    hotRecommendList: [
      {
        imgUrl: '../../images/no_image.png.png',
        url: '../productDetail/productDetail',
        name: '标题',
        price: '00.00',
      },
    ],
    hotRecommendCount : 0,
    discountList: [
    ],
    discountCount:0,
    specialBox: {
      url: '../discountDetail/discountDetail',
      banner: '../../images/products/banner_special1.png',
      list: [
        {
          imgUrl: '../../images/no_image.png',
          url: '../productDetail/productDetail',
          name: '标题',
          price: '00.00',
        },
      ],
    },
  },

  goToSearch: wxapp.handler(function* goToSearch(next){
    wx.navigateTo({
      url: '../search/search',
    });

    yield next;
  }),

  goToDiscountList: wxapp.handler(function* goToDiscount(next){
    wx.navigateTo({
      url: '../discountList/discountList',
    });

    yield next;
  }),

  goToDiscount: wxapp.handler(function* goToDiscount(next){
    wx.navigateTo({
      url: '../discountDetail/discountDetail',
    });

    yield next;
  }),

  goToDetail: wxapp.handler(function* goToDetail(next, e){
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });
    
    yield next;
  }),

  goToHotRecommend: wxapp.handler(function* goToHotRecommend(next){
    wx.navigateTo({
      url: '../recommendList/recommendList',
    });

    yield next;
  }),

  goToMenu: wxapp.handler(function* goToMenu(next, e){
    const url = e.currentTarget.dataset.url;
    wx.reLaunch({
      url,
    });

    yield next;
  }),

  onShow: wxapp.handler(function* onShow(next) {

    yield next;
  }),

  onLoad: wxapp.handler(function* onLoad(next) {

    yield next;

    var indexBannerApi = Api.getIndexBanner();
    var that = this;

    // 首页banner
    wx.request({
      url: indexBannerApi,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      complete: function (res) {
        that.setData({bannersList:res.data.data});
      }
    });

    // 首页拼团
    var getIndexList = Api.getIndexList();
    // 商品列表
    wx.request({
      url: getIndexList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {
        
      },
      complete: function (res) {
        that.setData({ productList: res.data.data });
      }
    });

    var getIndexRecommend = Api.getIndexRecommend();

    // 热门推荐
    wx.request({
      url: getIndexRecommend,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {

      },
      complete: function (res) {
        that.setData({ hotRecommendList: res.data.data, hotRecommendCount: res.data.count, discountList: res.data.pres, discountCount:res.data.pcount });
      }
    });

    var getCarNum = Api.getCarNum();
    const authTokenKey = Util.authTokenKey();
    const token = wx.getStorageSync(authTokenKey);
    wx.request({
      url: getCarNum,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {//发送给后台的数据
        token: token
      },
      complete: function (res) {
        that.setData({ carNum: res.data.data });
      }
    });


    // 专题
    var getSpecialList = Api.getSpecialList();
    wx.request({
      url: getSpecialList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {
        
      },
      complete: function (res) {
        that.setData({ special: res.data.data });
      }
    });

  }),

  onPullDownRefresh: wxapp.handler(function* onPullDownRefresh(next)   {
    var indexBannerApi = Api.getIndexBanner();
    var that = this;

    // 首页banner
    wx.request({
      url: indexBannerApi,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      complete: function (res) {
        that.setData({ bannersList: res.data.data });
      }
    });

    // 首页拼团
    var getIndexList = Api.getIndexList();
    // 商品列表
    wx.request({
      url: getIndexList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {

      },
      complete: function (res) {
        that.setData({ productList: res.data.data });
      }
    });

    var getIndexRecommend = Api.getIndexRecommend();

    // 热门推荐
    wx.request({
      url: getIndexRecommend,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {

      },
      complete: function (res) {
        that.setData({ hotRecommendList: res.data.data, hotRecommendCount: res.data.count });
      }
    });

    // 专题
    var getSpecialList = Api.getSpecialList();
    wx.request({
      url: getSpecialList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {

      },
      complete: function (res) {
        that.setData({ special: res.data.data });
      }
    });
    
    wx.stopPullDownRefresh();
    yield next;
  }),

});
